package com.ksp.wke.kspa04;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import static com.ksp.com.KSPCOM_BasConst.*;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

import javax.annotation.Resource;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ksp.api.KSPAPI_UserAgentUtil;
import com.ksp.api.KSPAPI_UsrAuth;
import com.ksp.dom.KSPDOM_ResDta;

import net.minidev.json.JSONArray;

@Controller
public class KSPA04Controller2 {

	private static ConcurrentLinkedQueue<Map<String, Object>> wArtScaQue=
			new ConcurrentLinkedQueue<>();
	
	private static AtomicInteger totalQueSize;
	
	private ExecutorService executorService = Executors.newCachedThreadPool();
	
	public static ReentrantLock queLock = new ReentrantLock(true);
	
	static {
		totalQueSize = new AtomicInteger(1000);
	}
	
	@Autowired
	private KSPA04Mapper kspa04Mapper;
	
	@Resource
	private CacheManager cacheManager;

	
	public KSPDOM_ResDta iniPam(ServletRequest request, HttpServletResponse response) {
		String wTtlTch = request.getParameter("TTL_TCH");
		if (wTtlTch == null || "".equals(wTtlTch)) {
			return KSPDOM_ResDta.buildFail("文章信息异常");
		}
		
		//String wUsrOam = KSPAPI_UsrAuth.getUsr(request);
		String wUsrAge = KSPAPI_UserAgentUtil.getUserAgenInfo(request);
		String wIP = KSPAPI_UserAgentUtil.getClientIP(request);
		//System.out.println(  wIP + wUsrAge);
		
		/*
		 * 
		 * 每个用户id，ip，每天访问只算一次，
		 * 
		 * ttlid + usrid + ip
		 * 
		 * 
		 * 
		 * */
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String wCurDay = sdf.format(new Date());
		String wArtTchKey = wTtlTch +  wIP + wCurDay;
		
	
		Cache wEhcArtHit = cacheManager.getCache("EhcArtHit");
		HashMap wScaMap = wEhcArtHit.get(EHK_SCA_MAP, HashMap.class);
		if (wScaMap == null || wScaMap.keySet().size() == 0) {
			wScaMap = new HashMap<>();
			wScaMap.put(wTtlTch, 1);
		}
	/*	ArrayList wScaList = wEhcArtHit.get(EHK_HIT_LST, ArrayList.class);
		if (wScaList == null || wScaList.size() == 0) {
			wScaList = new ArrayList<>();
			wScaList.add(wArtTchKey);
		}*/
		
		
		
		//文章id为key，浏览量为value，构成一个HashMap，
		
		//一个缓存里增加两个key，
		// 文章及浏览量的Map key EHC_ART_SCA_MAP
		// 文章id+oam+day 构成的标识list key EHC_HIT_TCH_LST
		//将文章id+用户id+ip 作为一个字符串存在一个list，方便判断已经访问
		//不存在，说明是新访客，需要加一，存在，则不需要增阿基浏览量
		/*if (!wScaList.contains(wArtTchKey)) {
			Integer wScaCnt = Integer.parseInt(wScaMap.get(wTtlTch).toString());
			wScaCnt++;
			wScaMap.put(wTtlTch, wScaCnt);
			wScaList.add(wArtTchKey);
		}*/
		//缓存数据之后
		wEhcArtHit.put(EHK_SCA_MAP, wScaMap);
		//wEhcArtHit.put(EHK_HIT_LST, wScaList);
		
		//放入消息队列里
		wScaMap.put("TTL_TCH", wTtlTch);
		wArtScaQue.add(wScaMap);
		
		SimpleDateFormat sfd = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
		
		System.out.println("主线程开始时间：" + sfd.format(new Date()));
		if (!ArtScaThread.dealLock.isLocked()) {
			ArtScaThread dealque = new ArtScaThread(wArtScaQue);
			executorService.execute(dealque);
		}
		
		
		
		List<Map<String, Object>> wResLst = new ArrayList<>();
		Map<String, Object> wResMap = new HashMap<>();
		wResMap.put("ART_CNT", Integer.parseInt(wScaMap.get(wTtlTch).toString()));
		wResLst.add(wResMap);
		KSPDOM_ResDta wResDta = new KSPDOM_ResDta();
		wResDta.setJdt(JSONArray.toJSONString(wResLst));
		wResDta.setCod("200");
		
		String datenow = sfd.format(new Date());
		System.out.println("主线程返回时间：" + datenow);
		return wResDta;
	}

	public KSPDOM_ResDta exeSrv(Map<String, Object> pPamMap) {
		return KSPDOM_ResDta.buildSucc("");
	}
}
